package com.cytangaa.display;

import java.lang.reflect.InvocationTargetException;

import com.cytanaa.youlike.R;
import com.cytangaa.displaysize.DisplaySize;
import com.cytangaa.youlike.database.DbAdapter;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.PluginState;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class HdPlayerHorizontal extends Activity {
	DisplaySize mDisplaySize;
	String SrcPath1 = "rtsp://v1.cache5.c.youtube.com/CiILENy73wIaGQkgHx9w8aRdHhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";
	String youtubeLink = null;
	String thumbnailLink, description, VideoId, viewCount, date = null;
	String title = null;
	WebView webview;
	public Button hd_basket_btn, hd_share_btn;
	String duration = null;

	DbAdapter db;
	
	/** Called when the activity is first created. */
	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i("horizontal", "in onCreate");

		// Remove notification bar
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		db = new DbAdapter(this);
		
		Log.i("horizontal", "in onCreate");

		getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
		ActionBar actionBar = getActionBar();
		actionBar.hide();
		setContentView(R.layout.hd_playerhorizontal);
		//setAutoOrientationEnabled(this.getContentResolver(), true);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

		Log.i("horizontal", "hd_playerhorizontal");

		Bundle bundle = this.getIntent().getExtras();
		SrcPath1 = bundle.getString("link");
		youtubeLink = bundle.getString("youtubeLink");
		thumbnailLink = bundle.getString("thumbnailLink");
		// description = bundle.getString("description");
		title = bundle.getString("title");
		VideoId = bundle.getString("id");
		duration = bundle.getString("duration");
		viewCount = bundle.getString("view_count");
		date = bundle.getString("uploaded_date");
		Log.v("horizontal", "duration is " + duration);
		Log.v("horizontal", "viewCount is " + viewCount);
		Log.v("horizontal", "VideoId is " + VideoId);

		webview = (WebView) findViewById(R.id.web_view_player_horizontal);
		webview.clearCache(true);
		webview.clearHistory();
		webview.getSettings().setJavaScriptEnabled(true);
		webview.getSettings().setPluginState(PluginState.ON);
		webview.getSettings().setPluginsEnabled(true);
		//webview.getSettings().setLoadWithOverviewMode(true);// completely zoomed out
		//webview.getSettings().setUseWideViewPort(true);
		webview.getTouchables();
		webview.setBackgroundColor(Color.parseColor("#00000000"));
		Log.v("horizontal", "after webview");
		Log.v("horizontal", "after webview");
		Log.v("horizontal", "after webview");
		Log.v("horizontal", "after webview");
		Log.v("horizontal", "after webview");

		/*
		 * code for initializing the layout
		 */
		Display display = getWindowManager().getDefaultDisplay();
		int[] widthHeight = new int[2];
		mDisplaySize = new DisplaySize(this);
		widthHeight = mDisplaySize.getDisplaySize(display);
		int height = widthHeight[1];

		LayoutParams lp = new RelativeLayout.LayoutParams(height*16/9, height);
		lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		lp.addRule(RelativeLayout.CENTER_VERTICAL);
		lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
		
		//TODO
		//getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		webview.setLayoutParams(lp);
		Log.v("horizontal", "after webview2");
		Log.v("horizontal", "after webview2");
		Log.v("horizontal", "after webview2");
		Log.v("horizontal", "after webview2");
		Log.v("horizontal", "after webview2");
		Log.v("horizontal", "after webview2");

		String emdLink = "http://www.youtube.com/embed/" + VideoId;

		webview.setWebChromeClient(new WebChromeClient());
		webview.setWebViewClient(new WebViewClient());
		Log.i("horizontal", " setWebViewClient" + emdLink);

		//webview.loadUrl("http://google.com");
		webview.loadUrl(emdLink);

	}
	
	public static void setAutoOrientationEnabled(ContentResolver resolver,
			boolean enabled) {
		Settings.System.putInt(resolver,
				Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
	}
	
	@Override
	public void onPause() {
		super.onPause();
		
		try {
			Log.i("in onpause", "onpause try" );
			Log.i("in onpause", "onpause try" );
			Class.forName("android.webkit.WebView")
					.getMethod("onPause", (Class[]) null)
					.invoke(webview, (Object[]) null);

		} catch (ClassNotFoundException cnfe) {
		} catch (NoSuchMethodException nsme) {
		} catch (InvocationTargetException ite) {
		} catch (IllegalAccessException iae) {
		}


	}
	
	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public void onRestart() {
		super.onRestart();
		Log.v("onRestart", "onRestart");
		//setAutoOrientationEnabled(this.getContentResolver(), true);
		String emdLink = "http://www.youtube.com/embed/" + VideoId;
        Log.v("onRestart", "portrait");

        setContentView(R.layout.hd_playerhorizontal);
        webview = (WebView) findViewById(R.id.web_view_player_horizontal);
        Log.v("onRestart", "find webview");

        /*
         * code for initializing the layout
         */
        
        mDisplaySize = new DisplaySize(this);
        Display display = getWindowManager().getDefaultDisplay();
        int[] widthHeight = new int[2];
        widthHeight = mDisplaySize.getDisplaySize(display);
        int height = widthHeight[1];
        Log.i("iframe", " before iframe");

		LayoutParams lp = new RelativeLayout.LayoutParams(height*16/9, height);
		lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		lp.addRule(RelativeLayout.CENTER_VERTICAL);
		lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
		
        webview.setLayoutParams(lp);
        webview.clearCache(true);
        webview.clearHistory();
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setPluginState(PluginState.ON);
        webview.getSettings().setPluginsEnabled(true);
        webview.getSettings().setLoadWithOverviewMode(true);// completely zoomed
                                                                                                                // out
        webview.getSettings().setUseWideViewPort(true);
        webview.getTouchables();
        // webview.getSettings().setBuiltInZoomControls(false);
        // webview.getSettings().setDisplayZoomControls(false);
        webview.setBackgroundColor(Color.parseColor("#00000000"));
        webview.setWebChromeClient(new WebChromeClient());
        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl(emdLink);
		
		Log.v("onResume", "onResume 2");
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		webview.clearDisappearingChildren();
		webview.stopLoading();
		webview.clearCache(true);
		webview.clearView();
		webview.destroyDrawingCache();
		webview.destroy();
		//setAutoOrientationEnabled(this.getContentResolver(), false);
		//Toast.makeText(this, "onStop", Toast.LENGTH_LONG).show();
	}
}
